package validators;

import dao.UserDAO;
import model.User;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.List;

@FacesValidator("validators.LoginValidator")
public class LoginValidator implements Validator {
    private UserDAO userDAO = new UserDAO();
    private List<User> users = userDAO.getAllUsers();
    @Override
    public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException {
        for(User user: users) {
            if(user.getLogin().equals(o.toString())) {
                FacesMessage msg = new FacesMessage("Sorry, this login is already taken!");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
        }
    }
}
